import pandas as pd
import numpy as np
import re

'''
f=pd.read_csv('result3.csv')
notNon=f['providesalary_text'].isnull().apply(lambda x: not x)
pattern = r'[0-9\.]+[0-9]?'
data=f['providesalary_text'].fillna('').str.findall(pattern)
w=f['providesalary_text'].fillna('').str.contains('万')
years=f['providesalary_text'].fillna('').str.contains('年')
f['providesalary_text']=data



def wDo(x):
    if len(x)==1:
        low=float(x[0])
        return [str(int(low*10))]
    elif len(x)==2:
        low=float(x[0])
        top=float(x[1])
        return [str(int(low*10)),str(int(top*10))]
    elif x==np.nan:
        return np.nan

def yearDo(x):
    if len(x)==1:
        low=float(x[0])
        return ['{:.1f}'.format(low/12)]
    elif len(x)==2:
        low=float(x[0])
        top=float(x[1])
        return ['{:.1f}'.format(low/12),'{:.1f}'.format(top/12)]
    elif x == np.nan:
        return np.nan

def addK(x):
    if len(x)==1:
        low=x[0]
        return [low+'K']
    elif len(x)==2:
        low=x[0]
        top=x[1]
        return [low+'k',top+'k']
    else:
        print(x)
        return ''


# print(f['providesalary_text'][w].apply(lambda x: wDo(x)).fillna(''))
wData = f['providesalary_text'][w].apply(lambda x: wDo(x))
ans = pd.merge(f,wData,left_index=True,right_index=True,how='outer')
yearData = ans['providesalary_text'][years].apply(lambda x: yearDo(x))
ans = pd.merge(f,yearData,left_index=True,right_index=True,how='outer')
# f['providesalary_text']=f['providesalary_text'][years].apply(lambda x: yearDo(x))
# f['providesalary_text']=f[notNon]['providesalary_text'].apply(lambda x: addK(x)).str.join('-')
ans.to_csv('result4.csv')
# print(data.apply(lambda x: yearDo(x)))
'''
f1=open('result3.csv', 'r', encoding='utf-8')
f2=open('result4.csv','w',encoding='utf-8')
f2.write('job_name,workarea_text,company_name,providesalary_text,educational_requirement,hands_on_background,num_recruits,issuedate,companytype_text,companysize_text,jobwelf\n')
for line in f1:
    if line == 'job_name,workarea_text,company_name,providesalary_text,educational_requirement,hands_on_background,num_recruits,issuedate,companytype_text,companysize_text,jobwelf\n':
        continue
    data=line.split(',')
    providesalary_text=data[3]
    pattern1=r'\d+\.\d+|\d+'
    pattern2=r'./.'
    findNums = re.findall(pattern1, providesalary_text)
    nums = [float(i) for i in findNums]
    if not nums:
        pass
    else:
        words=re.findall(pattern2, providesalary_text)[0].split('/')
        if words[0] == '万':
            nums = [int(10*i) for i in nums]
            # print(nums)
        if words[1] == '年':
            nums = [round(i/12) for i in nums]
        nums = [str(int(i))+'k' for i in nums]
    ans = '-'.join(nums)
    data[3]=ans
    f2.write(','.join(data))
f1.close()
f2.close()
